package cn.lystudio.rohmtimeopwide_background_v11.controller.webApi;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.PageUtil;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.MachineDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.MachinePO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.vo.LayuiPageApiResponse;
import cn.lystudio.rohmtimeopwide_background_v11.exceptions.JsonException;
import cn.lystudio.rohmtimeopwide_background_v11.service.MachineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@SaCheckLogin
@RequestMapping("/webapi/machine")
@Slf4j
public class MachineController {
    private MachineService machineService;

    @Autowired
    @Lazy
    public void setMachineService(MachineService machineService) {
        this.machineService = machineService;
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @GetMapping("/findAll")
    public LayuiPageApiResponse findAll(Integer page,Integer limit,@RequestParam(name = "machineNo",defaultValue = "")String machineNo){
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if (roleList.size() != 1) throw new JsonException(500, "权限异常");
        PageUtil.setFirstPageNo(1);
        List<MachineDTO> machineDTOList = machineService.getAll(machineNo);
        if(CollUtil.isEmpty(machineDTOList)) return LayuiPageApiResponse.ofSuccessData(null,0);

        return LayuiPageApiResponse.ofSuccessData(CollUtil.page(page,limit,machineDTOList),machineDTOList.size());
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptEnabled")
    public SaResult uptEnabled(Integer machineID, Boolean enabled) {
        return SaResult.data(machineService.uptMachineEnabled(machineID, enabled));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/delMachine")
    public SaResult delMachine(Integer machineID){
        return SaResult.data(machineService.delMachine(machineID));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/addMachine")
    public SaResult addMachine(@RequestBody MachinePO machine){
        return SaResult.data(machineService.addMachine(machine));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptMachine")
    public SaResult uptMachine(@RequestBody MachinePO machine){
        return SaResult.data(machineService.uptMachine(machine));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @GetMapping("/getMachineList4SecDept")
    public SaResult getMachineList4SecDept(Integer secDeptID){
        return SaResult.data(machineService.getMachineListBySecDeptID(secDeptID));
    }

    @GetMapping("/getMachineInfo")
    public SaResult getMachineInfo(String machineNo){
        return SaResult.data(machineService.getMachineDTOByMachineNo(machineNo));
    }
}
